package 力扣._00周赛._368周赛.问题2;

public class Solution {
    public int minimumSum(int[] nums) {
        int min = Integer.MAX_VALUE;
        boolean flag = false;
        //左最小，右最小
        int[] min_left = new int[nums.length];
        int[] min_right = new int[nums.length];
        min_left[1] = nums[0];
        for (int i = 2; i < nums.length; i++) { //下标i，i-1之间的最小元素
            min_left[i] = Math.min(min_left[i - 1], nums[i - 1]);
        }
        min_right[nums.length - 2] = nums[nums.length - 1];
        for (int i = nums.length - 3; i >= 0; i--) {
            min_right[i] = Math.min(min_right[i + 1], nums[i + 1]);
        }
        for (int i = 1; i < nums.length - 1; i++) {
            if (min_left[i] < nums[i] && nums[i] > min_right[i]) {
                flag = true;
                min = Math.min(min, min_left[i] + nums[i] + min_right[i]);
            }
        }
        if (flag)
            return min;
        return -1;
    }

}
